<?php
	$page = array('id' => $_REQUEST['pages_id']);
	
	if (local_post()) {
		$errors = array();
		if (empty($_POST['name'])) $errors[] = 'You must specify a Name for this page.';
		
		if (!empty($errors)) errors($errors);
		else {
			$page['uri'] = '/' . db_escape_string($_POST['uri']);
			$page['name'] = db_escape_string($_POST['name']);
			$page['published'] = intval($_POST['published']);
			$page['theme_file'] = db_escape_string($_POST['theme_file']);
			$page['private'] = intval($_POST['private']);
			
			//protect uri for negative id pages
			if ($page['id'] < 0) unset($page['uri']);
			
			$page = db_save($page, 'pages');
			
			foreach ($_POST['content'] as $idx => $content) {
				if ($content['module'] == '_!_') continue;
				
				if ($content['module'] == 'pages' || isset($content['html']) || isset($content['pages_html_id'])) {
					#it's an HTML content block
					$pages_html = array('id' => intval($content['pages_html_id']));
					$pages_html['html'] = $content['html'];
					$pages_html['pages_id'] = $page['id'];
					if ($content['purge'] == 1) $pages_html['purge'] = 1;
					
					$pages_html = db_save($pages_html, 'pages_html');
					
					$content['module'] = 'pages';
					$content['file'] = 'html';
					
					$content['params'] = "&pages_html_id={$pages_html['id']}";
				}
				
				if ($content['purge'] == 1) continue;
				
				$page['content'].= "module={$content['module']}&file={$content['file']}{$content['params']}\n";
			}
			
			#save the content field (must be after we already create all new records)
			$page = db_save($page, 'pages');
			
			if ($page['private'] == 1) {
				#set permissions
				#1. purge old perms
				db_query("DELETE FROM pages_to_user_roles WHERE pages_id = {$page['id']}");
				
				#2. insert new perms
				foreach ($_POST['user_roles'] as $user_roles_id) {
					db_query("INSERT INTO pages_to_user_roles (pages_id, user_roles_id) VALUES ({$page['id']}, $user_roles_id)");
				}
			}
			
			if ($_POST['button'] == 'Save and Close') location('index');
			elseif ($_POST['button'] == 'Save and New') location('edit');
		}
	}
	
	$page = db_select($page['id'], 'pages');
?>
<form action="?pages_id=<?php echo $page['id']?>" method="post">
	<fieldset>
		<legend>Page Details</legend>
		
		<label for="uri">
			URI*<br />
			http://<?php echo $_SERVER['HTTP_HOST']?>/<input type="text" name="uri" id="uri" value="<?php echo ltrim($page['uri'], '/')?>" style="display: inline-block;" <?php if($page['id'] < 0) echo 'readonly="readonly" '; ?> />
		</label>
		
		<label for="name">
			Name*
			<input type="text" name="name" id="name" value="<?php echo $page['name']?>" />
		</label>
		
		<label for="theme_file">
			Theme File <small>(leave blank for default)</small>
			<input type="text" name="theme_file" id="theme_file" value="<?php echo $page['theme_file']?>" />
		</label>
		
		<label for="published">
			<input type="checkbox" name="published" id="published" value="1" <?php if ($page['published'] == 1) echo 'checked="checked" '; ?>/>
			Published
		</label>
	</fieldset>
	
	<fieldset>
		<legend>Content</legend>
	<?php
		if ($page['id'] != 0) {
  		foreach (explode("\n", trim($page['content'])) as $idx => $query_string) {
  			$content = array('id' => $idx);
  			
  			foreach (explode('&', trim($query_string)) as $item) {
  				list($k, $v) = explode('=', $item);
  				if ($k == 'module' || $k == 'file' || $k == 'pages_html_id') $content[$k] = $v;
  				else $content['params'].= "&$k=$v";
  			}
				
				if (empty($content['module'])) continue;
  			
  	?>
  		<fieldset>
  			<legend><?php echo ($content['module'] == 'pages') ? 'HTML Content' : ucwords(str_replace('_', ' ' ,$content['module']))?></legend>
  
  			<input type="hidden" name="content[<?php echo $idx?>][module]" value="<?php echo $content['module']?>" />
  	<?php
  			if ($content['module'] == 'pages') {
  	?>
  			<input type="hidden" name="content[<?php echo $idx?>][pages_html_id]" value="<?php echo $content['pages_html_id']?>" />
  			<input type="hidden" name="content[<?php echo $idx?>][file]" value="index" />
  		<?php
			
          $oFCKeditor = new FCKeditor("content[$idx][html]");
          $oFCKeditor->BasePath = PHPDRIVER_WEB . 'plugins/fckeditor/';
          $oFCKeditor->Height = '500';
          $oFCKeditor->ToolbarSet = 'Default';
          $oFCKeditor->Value = db_field($content['pages_html_id'], 'pages_html', 'html');
          $oFCKeditor->Create();			
  			} else {
  		?>
  			<select name="content[<?php echo $idx?>][file]">
  				<option value=""> - Select One - </option>
  		<?php
  				foreach (scandir(PHPDRIVER . "modules/{$content['module']}") as $file) {
  					if (substr($file, 0, 1) == '.' || is_dir(PHPDRIVER . "modules/{$content['module']}/$file") || !stristr($file, '.php')) continue;
  					
  					$file = basename($file, '.php');
						$selected = ($file == $content['file']) ? ' selected="selected"' : '';
  		?>
  				<option value="<?php echo $file?>"<?php echo $selected?>><?php echo $file?></option>
  		<?php
  				}
  		?>
  			</select>
  			<input type="text" name="content[<?php echo $idx?>][params]" value="<?php echo $content['params']?>" />
  		<?php
  			}
  		?>
  			<br />
  			<input type="checkbox" name="content[<?php echo $idx?>][purge]" value="1" />
  			Delete
  		</fieldset>
  	<?php
			}//eof loop through existing content
		}//eof if page['id']
	?>
		
		<label for="new_content">
			Add Content to Page
			<select id="new_content" name="content[][module]">
				<option value="_!_">- Select One -</option>
			<?php
				$options = array();
			
				$sql = "SELECT name, folder FROM modules ORDER BY name ASC";
				$qry = db_query($sql);
				while ($module = db_fetch_assoc($qry)) {
					$options[$module['folder']] = ($module['folder'] == 'pages') ? 'HTML Content' : $module['name'];
				}
				
				asort($options);
				
				foreach ($options as $folder => $name) {
			?>
				<option value="<?php echo $folder?>"><?php echo $name?></option>
			<?php
				}
				
				unset($options);
			?>
			</select>
		</label>
		
		<input type="submit" name="button" value="Add Content" />
	</fieldset>
	
	<fieldset>
		<legend>Permissions</legend>
		
		<label for="private">
			<input type="checkbox" name="private" id="private" value="<?php echo $page['private']?>" />
			Private
		</label>
	<?php
		if ($page['private'] == 1) {
	?>
		<p>Select the Roles that have access to this page&hellip;</p>
		<ul>
		<?php
			$sql = "SELECT * FROM user_roles ORDER BY sort, name ASC";
			$qry = db_query($sql);
			while ($user_role = db_fetch_assoc($qry)) {
				$checked = (db_table_to_table($page['id'], $user_role['id'], 'pages_to_user_roles')) ? 'checked="checked" ' : '';
		?>
			<li>
				<label for="role_<?php echo $user_role['id']?>">
					<input type="checkbox" name="user_roles[]" value="<?php echo $user_role['id']?>" <?php echo $checked?>/>
					<?php echo $user_role['name']?>
				</label>
			</li>
		<?php
			}
		?>
		</ul>
	<?php
		}
	?>	
	</fieldset>
	
	<?php save_buttons(); ?>
</form>